const chatService = require('../services/chat');
const chatCode = require('../codes/chat');

module.exports = {

	/**
	 * [sendMessage 发送用户私聊消息]
	 * @param  {[type]} fromUser    [用户id]
	 * @param  {[type]} toUser 		[另一个用户id]
	 * @param  {[type]} message     [消息]
	 * @param  {[type]} time    	[时间]
	 * @return {[type]}             [description]
	 */
	async sendMessage(ctx) {
		let formData = ctx.request.body;
		let result = {
			success: false,
			message: '消息发送失败',
			data: null,
			code: ''
		};

		let chatResult = await chatService.sendMessage({
			from_user: formData.fromUser,
			to_user: formData.toUser,
			message: formData.message,
			time: new Date().getTime(),
			create_time: new Date().getTime(),
			update_time: new Date().getTime()
		});
		if(chatResult) {
			result.success = true;
			result.data = chatResult;
			result.message = '消息发送成功';
		}
		ctx.body = result;
	},

	/**
	 * [getMessage 获取用户私聊消息]
	 * @param  {[type]} fromUser    [用户id]
	 * @param  {[type]} toUser 		[另一个用户id]
	 * @param  {[type]} message     [消息]
	 * @return {[type]}             [description]
	 */
	async getMessage(ctx) {
		let formData = ctx.request.body;
		let result = {
			success: false,
			message: '消息获取失败',
			data: null,
			code: ''
		};

		let chatResult = await chatService.getMessage({
			from_user: formData.fromUser,
			to_user: formData.toUser,
		});
		if(chatResult) {
			result.success = true;
			result.data = chatResult;
			result.message = '消息获取成功';
		}
		ctx.body = result;
	},

};